/*********************************************************************
*                SEGGER Microcontroller GmbH & Co. KG                *
*        Solutions for real time microcontroller applications        *
**********************************************************************
*                                                                    *
*        (c) 1996 - 2015  SEGGER Microcontroller GmbH & Co. KG       *
*                                                                    *
*        Internet: www.segger.com    Support:  support@segger.com    *
*                                                                    *
**********************************************************************

** emWin V5.32 - Graphical user interface for embedded applications **
emWin is protected by international copyright laws.   Knowledge of the
source code may not be used to write a similar product.  This file may
only  be used  in accordance  with  a license  and should  not be  re-
distributed in any way. We appreciate your understanding and fairness.
----------------------------------------------------------------------
File        : GUIDEMO_Start.c
Purpose     : GUIDEMO initialization
----------------------------------------------------------------------
*/

#include "GUI.h"
#include "DIALOG.h"
#include "window.h"
#include "stm32f10x.h"
extern USART cout;
using namespace std;
Config_t Config =
{
	"UFO.bmp","Chobits"
};
GUI_FONT UTF_8;
GUI_XBF_DATA UTF_8_Data;
/*********************************************************************
*
*       MainTask
*/
#define ID_BUTTON_DIS (GUI_ID_USER+0)
void _cb(WM_MESSAGE* pMsg)
{
	switch (pMsg->MsgId)
	{
	case WM_PAINT:
	{
		char Path[64] = "0:/Pic/";
		strcat(Path, Config.DeskName);
		res = f_open(&fil, Path, FA_READ);
		if (res != FR_OK)
			cout << "file open fail" << endl << "res:" << res << endl << __FILE__ << ':' << __LINE__ << endl;
		GUI_BMP_DrawEx(GetPic, &fil, 0, 0);
		f_close(&fil);
		//      GUI_SetBkColor(GUI_BLUE);
		//      GUI_Clear();
		break;
	}
	default:
		WM_DefaultProc(pMsg);
	}
}
void LoadConfig()
{
	uint32_t num;
	res = f_open(&fil, "0:/config", FA_READ);
	if (res != FR_OK)
		cout << "file open fail" << endl << "res:" << res << endl << __FILE__ << ':' << __LINE__ << endl;
	res = f_read(&fil, &Config, sizeof(Config), &num);
	if (res != FR_OK)
		cout << "file read fail" << endl << "res:" << res << endl << __FILE__ << ':' << __LINE__ << endl;
	f_close(&fil);
}

int _cbXBFGetdData(U32 Off, U16 NumBytes, void* pVoid, void* pBuffer)
{
	uint32_t num;
	FIL* Font = (FIL*)pVoid;
	res = f_lseek(Font, Off);
	if (res != FR_OK)
	{
		cout << "f_lseek fail" << endl << "res:" << res << endl << __FILE__ << ':' << __LINE__ << endl;
		return 1;
	}
	res = f_read(Font, pBuffer, NumBytes, &num);
	if (res != FR_OK)
	{
		cout << "file read fail" << endl << "res:" << res << endl << __FILE__ << ':' << __LINE__ << endl;
		return 1;
	}
	return 0;
}
void MainTask()
{
	GUI_Init();
	res = f_open(&font, "0:/Font/CN16.xbf", FA_READ);
	if (res != FR_OK)
		cout << "font open Fail:" << res << endl;
	GUI_XBF_CreateFont(&UTF_8, &UTF_8_Data, GUI_XBF_TYPE_PROP_EXT, _cbXBFGetdData, &font);
	GUI_UC_SetEncodeUTF8();

	GUI_EnableAlpha(1);
	WM_SetCallback(WM_HBKWIN, _cb);
	LoadConfig();
	CreateMainWindow();
}

/*************************** End of file ****************************/

